//
// Created by yanhai on 2021/3/29.
//

#include <iostream>
#include <unistd.h>
#include "timewheel.h"

struct node_ctx {
    node_ctx(uint32_t _interval,
             uint32_t _thread_id) : interval(_interval),
                                    thread_id(_thread_id) {}

    uint32_t interval;
    uint32_t thread_id;
};

void single_thread_test()
{
    timer T;
    bool quit = false;

    T.add_timer(6000, [&quit](void *arg) {
        quit = true;
        std::cout << "good bye!!!" << std::endl;
    }, nullptr);

    auto node_cb = [](void *arg) {
        auto n = static_cast<node_ctx *>(arg);
        std::cout << "interval " << n->interval
                  << " 到期了... thread_id:" << n->thread_id
                  << std::endl;
        delete n;
    };

    T.add_timer(300, node_cb, new node_ctx(300, 0));
    T.add_timer(4000, node_cb, new node_ctx(4000, 0));
    T.add_timer(1100, node_cb, new node_ctx(1100, 0));
    T.add_timer(3333, node_cb, new node_ctx(3333, 0));

    while (!quit) {
        T.expire_timer();
        usleep(2500); // 2.5ms
    }
}

void multi_thread_test()
{

}

int main()
{
    single_thread_test();
//    multi_thread_test();
    return 0;
}
